#!/bin/bash
# macOS 网络配置手动备份脚本
# 使用方法: chmod +x manual_network_backup.sh && ./manual_network_backup.sh

# 创建备份目录
BACKUP_DIR="network_backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"

echo "开始备份macOS网络配置..."
echo "备份目录: $BACKUP_DIR"

# 1. 备份网络接口配置
echo "1. 备份网络接口配置..."
ifconfig -a > "$BACKUP_DIR/ifconfig.txt"
networksetup -listallnetworkservices > "$BACKUP_DIR/network_services.txt"

# 备份每个网络服务的详细配置
networksetup -listallnetworkservices | tail -n +2 | while read service; do
    if [ ! -z "$service" ]; then
        echo "备份服务: $service"
        networksetup -getinfo "$service" > "$BACKUP_DIR/service_${service// /_}.txt"
    fi
done

# 2. 备份DNS配置
echo "2. 备份DNS配置..."
scutil --dns > "$BACKUP_DIR/dns_config.txt"
cat /etc/resolv.conf > "$BACKUP_DIR/resolv_conf.txt"

# 3. 备份路由表
echo "3. 备份路由表..."
netstat -rn > "$BACKUP_DIR/routing_table.txt"
arp -a > "$BACKUP_DIR/arp_table.txt"

# 4. 备份代理设置
echo "4. 备份代理设置..."
networksetup -getwebproxy "Wi-Fi" > "$BACKUP_DIR/web_proxy.txt" 2>/dev/null
networksetup -getsecurewebproxy "Wi-Fi" > "$BACKUP_DIR/secure_web_proxy.txt" 2>/dev/null
networksetup -getsocksfirewallproxy "Wi-Fi" > "$BACKUP_DIR/socks_proxy.txt" 2>/dev/null

# 5. 备份防火墙设置
echo "5. 备份防火墙设置..."
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate > "$BACKUP_DIR/firewall_state.txt" 2>/dev/null
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --listapps > "$BACKUP_DIR/firewall_rules.txt" 2>/dev/null

# 6. 备份hosts文件
echo "6. 备份hosts文件..."
cp /etc/hosts "$BACKUP_DIR/hosts.txt"

# 7. 备份网络偏好设置
echo "7. 备份网络偏好设置..."
if [ -d "$HOME/Library/Preferences/SystemConfiguration" ]; then
    cp -r "$HOME/Library/Preferences/SystemConfiguration" "$BACKUP_DIR/"
fi

# 8. 记录Redis服务器信息（不进行连接测试）
echo "8. 记录Redis服务器信息..."
REDIS_HOST="test-redis.oyumwu.clustercfg.apne1.cache.amazonaws.com"
echo "目标Redis服务器: $REDIS_HOST" > "$BACKUP_DIR/redis_server_info.txt"
echo "记录时间: $(date)" >> "$BACKUP_DIR/redis_server_info.txt"
echo "" >> "$BACKUP_DIR/redis_server_info.txt"
echo "注意: 此Redis服务器需要公钥认证，请先配置本地公钥后联系运维添加到白名单" >> "$BACKUP_DIR/redis_server_info.txt"

# 9. 创建恢复脚本
echo "9. 创建恢复脚本..."
cat > "$BACKUP_DIR/restore.sh" << 'EOF'
#!/bin/bash
# macOS 网络配置恢复脚本

echo "开始恢复网络配置..."

# 恢复hosts文件
if [ -f "hosts.txt" ]; then
    echo "恢复hosts文件..."
    sudo cp hosts.txt /etc/hosts
fi

# 恢复网络偏好设置
if [ -d "SystemConfiguration" ]; then
    echo "恢复网络偏好设置..."
    sudo cp -r SystemConfiguration/* ~/Library/Preferences/SystemConfiguration/
fi

# 重启网络服务
echo "重启网络服务..."
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

echo "网络配置恢复完成！"
echo "请重启系统以确保所有设置生效。"
EOF

chmod +x "$BACKUP_DIR/restore.sh"

# 10. 创建备份信息文件
echo "10. 创建备份信息文件..."
cat > "$BACKUP_DIR/backup_info.txt" << EOF
macOS 网络配置备份信息
========================
备份时间: $(date)
备份目录: $BACKUP_DIR
备份内容:
- 网络接口配置 (ifconfig.txt)
- 网络服务配置 (network_services.txt)
- DNS配置 (dns_config.txt, resolv_conf.txt)
- 路由表 (routing_table.txt, arp_table.txt)
- 代理设置 (web_proxy.txt, secure_web_proxy.txt, socks_proxy.txt)
- 防火墙设置 (firewall_state.txt, firewall_rules.txt)
- hosts文件 (hosts.txt)
- 网络偏好设置 (SystemConfiguration/)
- Redis服务器信息 (redis_server_info.txt)

恢复方法:
1. 进入备份目录: cd $BACKUP_DIR
2. 运行恢复脚本: ./restore.sh
3. 重启系统

注意事项:
- 恢复前请确保系统处于安全状态
- 建议在恢复前再次备份当前配置
- 如果遇到问题，可以手动恢复各个配置文件
EOF

echo ""
echo "✅ 备份完成！"
echo "备份位置: $BACKUP_DIR"
echo "恢复方法: cd $BACKUP_DIR && ./restore.sh"
echo "备份信息: $BACKUP_DIR/backup_info.txt"
echo ""
echo "Redis服务器信息已记录: $REDIS_HOST"
echo "注意: 需要先配置公钥认证才能连接"
